package com.fxsh168.server.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fxsh168.server.base.BaseActivityForClose;
import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class BindAlipay extends BaseActivity {

    private EditText mPutZhiFubao;
    private Button mLijiBind;
    private String mGetZhiFuBao, mPhone, token;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_alipay);
        mPutZhiFubao = (EditText) findViewById(R.id.putZhiFubao);

        SharedPreferences sp = getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");

        mLijiBind = (Button) findViewById(R.id.lijiBind);
        BaseActivityForClose.activityStack.push(this);
    }


    //绑定支付宝
    public void lijiBindZhiFuBao(View view) {
        //获取输入的支付宝账号
        mGetZhiFuBao = mPutZhiFubao.getText().toString().trim();
        //提交规则
        //参数：phone 手机号
        //token
        //playzfb  支付宝账号
        if (!TextUtils.isEmpty(mGetZhiFuBao)) {
            OkHttpUtils.post().addParams("phone", mPhone).addParams("token", token).addParams("playzfb", mGetZhiFuBao)
                    .url(Constant.ZHIFUBAOBANGDING).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    Toast.makeText(BindAlipay.this, "连接不上服务器!", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onResponse(String response, int id) {
                    if (response != null) {
                        RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                        String code = regiYZMCG.getCode().toString().trim();
                        if (code.equals(200 + "")) {
                            Toast.makeText(BindAlipay.this, "绑定成功!", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(BindAlipay.this, BindAcountActivity.class);
                            intent.putExtra("isBindZhiFuBao", true);
                            intent.putExtra("playzfb", mGetZhiFuBao);
                            BindAlipay.this.setResult(Constant.YIJINGBINDZHIFUBAO, intent);
                            BindAlipay.this.finish();
                        } else if (code.equals(201 + "")) {
                            Toast.makeText(BindAlipay.this, "非法请求!", Toast.LENGTH_SHORT).show();
                        } else if (code.equals(202 + "")) {
                            Toast.makeText(BindAlipay.this, "此账号已经被绑定!", Toast.LENGTH_SHORT).show();
                        } else if (code.equals(203 + "")) {
                            Toast.makeText(BindAlipay.this, "非法请求!", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(BindAlipay.this, "数据为空!", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            mPutZhiFubao.setError("支付宝账户不能为空!");
        }
    }
}
